<?php
class App_IndexController extends Zend_Controller_Action
{
   public function init(){
      $this->request = $this->getRequest();
      $this->formData = $this->request->getPost();
      $this->loginForm = new App_Form_LoginForm();
      
      $css = new App_Model_Pulpit();
      $this->view->css = $css->pulpitcss();

      
      

   }
   public function indexAction(){

		if($this->_getParam('error')){
			echo $this->_getParam('error');
			$this->view->error = $this->_getParam('error');
			$this->loginForm->populate($this->formData);      
		}

        if($this->_request->isPost() && $this->_getParam('error') == null) {
            $this->formData = $this->request->getPost();

            if($this->loginForm->isValid($this->formData)  && !Zend_Auth::getInstance()->hasIdentity()) {
                $this->_forward('auth');
            } else {
                $this->loginForm->populate($this->formData);
            }
        }

        $this->view->form = $this->loginForm;
   }

   public function authAction(){
    	$this->_helper->viewRenderer->setNoRender(true);    	//zablokowanie renderowania widoku
    	//brak mozliwosci przejscia do tej akcji bezposrednio przez link
    	if( $this->request->getParam('controller') == $this->request->getControllerName()
            && $this->request->getParam('action') == $this->request->getActionName()) {
            return $this->_helper->redirector('index');
        }
    	if(Zend_Auth::getInstance()->hasIdentity()){        //jesli uzytkownik jest aktualnie zalogowany
    		$this->_helper->redirector('index');
    	}
    	$dbAdapter = new Zend_Db_Table();
    	$authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter->getAdapter(), 'uzytkownik');
    	$authAdapter->setTableName('uzytkownik')
    				->setIdentityColumn('login')
    				->setCredentialColumn('haslo_hash');
    				//->setCredentialTreatment();
    			
    	$login = $this->formData['input_login'];
    	$password = $this->formData['input_password'];
    		      
    	$authAdapter->setIdentity($login);
	$authAdapter->setCredential(md5($password));

	    Zend_Session::rememberMe(90000);
	    
	    $auth = Zend_Auth::getInstance();
	    $result = $auth->authenticate($authAdapter);
	    
	    if($result->isValid()){
	    	
	    	$kontoId = $authAdapter->getResultRowObject(null)->id;
                $rolaId = $authAdapter->getResultRowObject(null)->id_rola;
	    	
	    	$rola = new App_Model_Role();    	
                $rola = $rola->getRola($kontoId);

		$auth->getStorage()->write((object)
		    array(
		            'dane' => $authAdapter->getResultRowObject(array('id','login','imie','nazwisko','id_rola')),
	                    'rola'      => $rola['id'],
                            'kod_roli'  => $rola['kod_roli']
		    ));
      		$log = new App_Model_Logs();
                $log->login();

	    	//if(App_Model_Auth::isAdmin())
	    	//	$this->_helper->redirector('index', null, 'admin');
	    	//else
	    	//	$this->_helper->redirector('index', null, 'app');
	    	
	    } else {
	    	$this->_forward('index',null,null, array('error' => 'Błędne hasło lub login'));
	    }
    }
   public function logoutAction(){
        $log = new App_Model_Logs();
    	$log->logout();
    	Zend_Auth::getInstance()->clearIdentity();
	    $this->_helper->redirector('index', 'index', 'app');
    }

    public function headAction() {
    	
    	 	
    	
    }
    
    public function bodyAction(){
    	
    	
    	
    } 
    
    public function footAction(){
    	
    	
    	
    }

}
